package v1

import (
	"ginblog/model"
	errMsg "ginblog/model/err"
	"ginblog/model/request"
	"ginblog/model/response"
	"ginblog/service"
	"github.com/gin-gonic/gin"
	"strconv"
)

// @Summary 添加分类
// @Description 添加分类
// @Tags 分类
// @Accept application/json
// @Produce application/json
// @Security ApiKeyAuth
// @Param data query int true "参数"
// @Success 200 {object} response.Res
// @Router /api/v1/category [post]
func AddCate(c *gin.Context) {
	var (
		code errMsg.Code
		data model.Category
	)
	_ = c.ShouldBindJSON(&data)
	// todo 参数校验
	if code = service.AddCategory(&data); code != errMsg.SUCCESS {
		response.Fail(c, code, nil)
		return
	}
	response.Ok(c, nil)
}

// @Summary 删除分类
// @Description 功能描述
// @Tags 分类
// @Accept application/json
// @Produce application/json
// @Security ApiKeyAuth
// @Param data query int true "参数"
// @Success 200 {object} response.Res
// @Router /api/v1/category [delete]
func DeleteCate(c *gin.Context) {
	var data model.Category
	id, _ := strconv.Atoi(c.Param("id"))
	data.ID = uint(id)
	if code := service.DeleteCategory(&data); code != errMsg.SUCCESS {
		response.Fail(c, code, nil)
		return
	}
	response.Ok(c, nil)
}

func EditCate(c *gin.Context) {
	var (
		cate model.Category
		data map[string]interface{}
	)
	id, _ := strconv.Atoi(c.Param("id"))
	cate.ID = uint(id)
	_ = c.ShouldBindJSON(&data)
	if code := service.UpdateCategory(&cate, data); code != errMsg.SUCCESS {
		response.Fail(c, code, nil)
		return
	}
	response.Ok(c, nil)
}

func GetCate(c *gin.Context) {
	var (
		data *model.Category
		code errMsg.Code
	)
	id, _ := strconv.Atoi(c.Param("id"))
	if data, code = service.GetCategory(id); code != errMsg.SUCCESS {
		response.Fail(c, code, nil)
	}
	response.Ok(c, data)
}
func GetCateList(c *gin.Context) {
	info, code := service.GetCateList(request.GetPageParam(c))
	if code != errMsg.SUCCESS {
		response.Fail(c, code, nil)
		return
	}
	response.Ok(c, info)
}
